<template>
  <el-container>
    <el-main>
      <div v-if="houseinfo.length">
        <el-row
          class="houselist"
          v-for="(item, index) in houseinfo"
          :key="index"
        >
          <el-col :span="24">
            <div
              class="block"
              @click="toHouseOrComment(tableData[index].commentIs, item.id)"
            >
              <el-image
                v-if="item.firstImage"
                style="width: 200px; height: 137px"
                :src="item.firstImage"
                :lazy="true"
              ></el-image>

              <div class="info">
                <p class="title">{{ item.title }}</p>
                <p class="detail">
                  {{ item.bedroom }}室{{ item.living }}厅|{{ item.area }}平米|{{
                    item.floor
                  }}层（共{{ item.allfloor }}层） {{ item.mastername }}
                </p>
                <p class="site">{{ item.houseSite }}</p>
              </div>

              <div class="money">
                <p>
                  <strong>{{ item.money }}</strong>
                  元/月
                </p>
              </div>
              <el-dialog
              title="提示"
              :visible.sync="dialogVisible"
              width="30%"
              :before-close="handleClose"
            >
              <span>租房已结束，请评价</span>
              <span slot="footer" class="dialog-footer">
                <!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
                <el-button
                  type="primary"
                  @click="
                    $router.push('/evaluate?houseAccount=' + item.houseAccount + '&id=' + tableData[index].id),
                      (dialogVisible = false)
                  "
                  >下一步</el-button
                >
              </span>
            </el-dialog>
            </div>
          </el-col>
        </el-row>
        <div style="display: flex; justify-content: center">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
      <div v-else><nodata></nodata></div>
    </el-main>
  </el-container>
</template>


<script>
import request from "@/utils/request";
import nodata from "../../components/nodata.vue";
export default {
  components: {
    nodata,
  },
  data() {
    return {
      dialogVisible: false,
      user: localStorage.getItem("user")
        ? JSON.parse(localStorage.getItem("user"))
        : {},
      total: 0,
      pageNum: 1,
      pageSize: 5,
      tableData: [],
      houseinfo: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    async load() {
      const res = await request.get(`/history/page/${this.user.userName}`);
      if (res.code === 200) {
        this.tableData = res.data.records;
        console.log(this.tableData);
        this.total = res.data.total;
        for (let i = 0; i < this.tableData.length; i++) {
          const hosueRes = await request.get(
            `/house/account/${this.tableData[i].houseAccount}`
          );
          if (hosueRes.code === 200) {
            this.houseinfo.push(hosueRes.data);
          }
        }
        for (let j = 0; j < this.houseinfo.length; j++) {
          const imgRes = await request.get(
            `/house/firstimg/${this.houseinfo[j].houseAccount}`
          );
          const userRes = await request.get(
            `/user/${this.houseinfo[j].ownerId}`
          );
          if (imgRes.code === 200) {
            this.$set(this.houseinfo[j], "firstImage", imgRes.data);
          }
          if (userRes.code === 200) {
            this.$set(this.houseinfo[j], "mastername", userRes.data.nikeName);
          }
        }
      } else {
        this.$notify.error(res.msg);
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load();
    },
    toHouseOrComment(commentIs, id) {
      console.log(commentIs);
      if (commentIs) {
        this.$router.push("/housedetail?id=" + id);
      } else {
        this.dialogVisible = true;
      }
    },
  },
};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /* background-color: #e9eef3; */
  color: #333;
  text-align: center;
  /* line-height: 160px; */
  overflow: hidden;
  margin-top: -20px !important;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.search_area {
  width: 100%;
  min-width: 1150px;
  height: 150px;
  padding: 25px 0;
  background: #f5f5f6;
  box-sizing: border-box;
  z-index: 5;
  position: relative;
}
.belike_nav {
  width: 1150px;
  height: 30px;
  margin: 0 auto 30px 300px;
  position: absolute;
}
.belike_nav .belike_nav_tab,
.belike_nav .belike_nav_tab li {
  display: inline-block;
  vertical-align: bottom;
  position: relative;
}
.green_logo {
  position: absolute;
  color: #007e63;
  margin-left: 37px;
}
.green_logo i {
  font-size: 50px;
}

.belike_nav .belike_nav_tab ul {
  margin-left: 73px;
}
.belike_nav .belike_nav_tab li {
  line-height: 25px;
  font-size: 16px;
  margin-right: 30px;
  position: relative;
  font-weight: 700;
  list-style: none;
}

.search {
  margin-top: 65px;
  margin-bottom: 24px;
  position: absolute;
  width: 1150px;
  margin-left: 300px;
}
.search .search__wrap {
  display: inline-block;
  width: 710px;
  line-height: 46px;
  font-size: 14px;
  border-radius: 2px;
}
.el-input {
  width: 400px !important;
}
/* 房源 */
.houselist {
  /* margin-bottom: 20px; */
  background-color: #fff;
  border-bottom: 1px dashed #e6e6e6;
  cursor: pointer;
  /* width: 900px; */
  width: 800px;
  height: 184px;
  padding: 20px;
  position: relative;
  /* border-top: 1px dashed black; */
  margin-left: -430px;
  left: 50%;
}
.el-col {
  border-radius: 4px;
}
/* .bg-purple-dark {
  background: #99a9bf;
} */
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
/* .grid-content {
  border-radius: 4px;
  min-height: 36px;
} */

.block {
  position: relative;
}
.el-image {
  position: absolute;
  display: inline-block;
  overflow: hidden;
  margin-left: -50%;
  margin-top: 4px;
  /* margin-top: -9px; */
}
.info {
  position: absolute;
  color: #797979;
  /* float: left; */
  left: 30%;
  font-size: 14px;
  overflow: hidden;
  /* padding-left: 20px; */
  width: 460px;
  text-align: left;
  font-family: Microsoft YaHei, 微软雅黑, Hiragino Sans GB, tahoma, arial,
    simhei;
}
.title {
  color: #333;
  font-size: 20px;
  font-weight: 700;
  font-family: "Microsoft YaHei";
}
.detail {
  font-size: 15px;
  font-weight: 700;
  color: #333;
}
.site {
  color: #666;
  font-size: 14px;
}
.money {
  position: absolute;
  float: right;
  width: 112px;
  right: 5%;
  margin-top: 5%;
  color: #eb5f00;
  font-size: 20px;
  font-weight: 700;
}
</style>